package com.jeff.tianti.controller;

import com.jeff.tianti.cms.dto.font.IJobQueryDTO;
import com.jeff.tianti.cms.entity.Area;
import com.jeff.tianti.cms.entity.Company;
import com.jeff.tianti.cms.entity.Job;
import com.jeff.tianti.cms.entity.Resume;
import com.jeff.tianti.cms.service.AreaService;
import com.jeff.tianti.cms.service.CompanyService;
import com.jeff.tianti.cms.service.JobService;
import com.jeff.tianti.cms.service.ResumeService;
import com.jeff.tianti.common.dto.AjaxResult;
import com.jeff.tianti.common.entity.BaseEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/job")//企业发布职位表
public class JobController {
    @Autowired
    private JobService jobService;

    /**
     * @Ceator:liuYI
     * @Describe:获取人才天地职位列表
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public AjaxResult jobList(@RequestBody IJobQueryDTO dto) {
        Page<Job> page = jobService.findAllNoDel(dto);
        return AjaxResult.setSuccessData(page);
    }

    @Autowired
    private AreaService areaService;
    @Autowired
    private CompanyService companyService;

    /**
     * @Ceator:liuYI
     * @Describe:人才天地发布职位
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult saveJob(@RequestBody Job newJob) {
        AjaxResult ajaxResult = new AjaxResult();
        ajaxResult.setSuccess(false);
        String id = newJob.getId();
        if (StringUtils.isNotBlank(id)) {//修改
            Job oldJob = jobService.find(id);
            Integer areaId = oldJob.getAreaId();
            Integer areaId1 = newJob.getAreaId();
            //更新地区数据
            if (!areaId.equals(areaId1)) {
                Area area = areaService.find(areaId);
                newJob.setLongitud(area.getLongitud());
                newJob.setLatitude(area.getLatitude());
            }
            Company company = updateCompany(newJob);
            //            job表塞入公司信息
            newJob.setCompanyName(company.getCompanyName());
            newJob.setCompanyId(company.getId());
            jobService.update(newJob);
        } else { //添加
            if (StringUtils.isBlank(newJob.getUid())) {
                throw new RuntimeException("uid:参数缺失");
            }
            Area area = areaService.find(newJob.getAreaId() );
            newJob.setLongitud(area.getLongitud());
            newJob.setLatitude(area.getLatitude());
//            job表塞入公司信息
            Company company = updateCompany(newJob);
            newJob.setCompanyName(company.getCompanyName());
            newJob.setCompanyId(company.getId());
            newJob.setAuditFlag(BaseEntity.AUDIT_FLAG_NOT);
            newJob.setDeleteFlag(BaseEntity.DELETE_FLAG_NORMAL);
            newJob.setCreateDate(new Date());
            jobService.save(newJob);
        }
//        更新公司热招职位
        return AjaxResult.setSuccessData(null);
    }

    @Autowired
    private ResumeService resumeService;


    /**
     * @Ceator:liuYI
     * @Describe:人才天地获取职位详情
     */
    @RequestMapping(value = "/details", method = RequestMethod.GET)
    public AjaxResult findJobById(@RequestParam("id") String id, @RequestParam("uid") String uid) {
        if (StringUtils.isBlank(id)) {
            throw new RuntimeException("id:参数为空");
        } else if (StringUtils.isBlank(uid)) {
            throw new RuntimeException("uid:参数为空");
        }
//        获取职务和公司信息
        Job job = jobService.find(id);
//        获取个人简历信息
        List<Resume> resumes = resumeService.findByUid(uid);
//        获取公司信息
        Company company = companyService.findByUidMes(job.getUid());
        Map<String, Object> map = new HashMap<>();
        map.put("resume", resumes);
        map.put("job", job);
        map.put("company", company);
        return AjaxResult.setSuccessData(map);
    }

    private Company updateCompany(Job newJob) {
        Company company = companyService.findComByUid(newJob.getUid());
        company.setMoneyMin(newJob.getMoneyMin());
        company.setMoneyMax(newJob.getMoneyMax());
        company.setState(newJob.getState());
        company.setReZhao(newJob.getJobName());
        company.setUpdateDate(new Date());
        companyService.update(company);
        return company;
    }
}
